package com.atguigu.yygh.host.testmongo;

import com.mongodb.client.result.DeleteResult;
import com.mongodb.client.result.UpdateResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.mongodb.core.query.Update;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.regex.Pattern;

/**
 * @author DongYu Zhou
 * @date 2022/6/22 21:24
 */
@RestController
@RequestMapping("/mongo1")
public class TestMongo1 {

    @Autowired
    private MongoTemplate mongoTemplate;

    //添加
    @RequestMapping("/create")
    public void createUser() {
        User user = new User();
        user.setAge(20);
        user.setName("test");
        user.setEmail("4932200@qq.com");
        User insert = mongoTemplate.insert(user);
        System.out.println("insert = " + insert);
    }

    //查询所有
    @RequestMapping("/getAll")
    public List<User> getAll() {
        List<User> userList = mongoTemplate.findAll(User.class);
        userList.forEach(System.out::println);
        return userList;
    }

    //根据id查询
    @RequestMapping("/getById")
    public User getById() {
        System.out.println(mongoTemplate.findById("62b30f066a09e0a28b96cf7a", User.class));
        return mongoTemplate.findById("62b30f066a09e0a28b96cf7a", User.class);

    }

    //条件模糊查询
    @GetMapping("findUserLike")
    public List<User> findUserLike() {
        String name = "est";
        String regex = String.format("%s%s%s", "^.*", name, ".*$");
        Pattern pattern = Pattern.compile(regex, Pattern.CASE_INSENSITIVE);
        Query query = new Query(//条件筛选
                Criteria.where("name").regex(pattern)
        );
        List<User> users = mongoTemplate.find(query, User.class);
        users.forEach(System.out::println);
        return users;
    }

    //条件带分页模糊查询
    @GetMapping("findUserLikePage")
    public Map<String,Object> findUserLikePage() {

        HashMap<String, Object> hashMap = new HashMap<>();
        int pageNum=1;  //当前页
        int pageSage=10; //每页显示几条

        String name = "est";
        String regex = String.format("%s%s%s", "^.*", name, ".*$");
        Pattern pattern = Pattern.compile(regex, Pattern.CASE_INSENSITIVE);
        Query query = new Query(//条件筛选
                Criteria.where("name").regex(pattern)
        );
        long total = mongoTemplate.count(query, User.class);
        hashMap.put("total",total);
        query.skip((pageNum-1)*pageSage).limit(pageSage);

        List<User> users = mongoTemplate.find(query, User.class);
        hashMap.put("users",users);
        System.out.println("hashMap = " + hashMap);
        return hashMap;
    }

    //修改
    @RequestMapping("/updateUs")
    public void updateUs(){

        User user = mongoTemplate.findById("62b3192f5876f7276a31cba0", User.class);
        user.setName("test_1");
        user.setAge(25);
        user.setEmail("493220990@qq.com");

        Query query = new Query(
                Criteria.where("_id").is(user.getId())
        );
        Update update = new Update();
        update.set("name", user.getName());
        update.set("age", user.getAge());
        update.set("email", user.getEmail());

        UpdateResult result = mongoTemplate.upsert(query, update, User.class);
        long matchedCount = result.getMatchedCount();
        System.out.println("matchedCount = " + matchedCount);
    }

    //删除
    @RequestMapping("/deleteUser")
    public void deleteUser(){

        Query query = new Query(
                Criteria.where("name").is("lisi")
        );

        DeleteResult remove = mongoTemplate.remove(query, User.class);
        long deletedCount = remove.getDeletedCount();
        System.out.println("deletedCount = " + deletedCount);

    }


}
